﻿using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Diagnostics;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Navigation;
using BlogApp.Data.Models;
using BlogApp.WP8.Resources;
using BlogApp.WP8.ViewModel;
using Microsoft.Phone.Controls;
using Microsoft.Phone.Shell;
using Microsoft.Practices.ServiceLocation;
using GestureEventArgs = System.Windows.Input.GestureEventArgs;

namespace BlogApp.WP8.View
{
    public partial class CategoryPage : PhoneApplicationPage
    {
        private CategoryViewModel _vm;
        public CategoryPage()
        {
            InitializeComponent();

            _vm = (CategoryViewModel)DataContext;
        }

        protected override void OnNavigatedTo(NavigationEventArgs e)
        {
            ((ApplicationBarIconButton)ApplicationBar.Buttons[0]).Text = AppResources.AppBarButtonTextSearch;
            base.OnNavigatedTo(e);
        }

        private void OnPostTapped(object sender, GestureEventArgs e)
        {
            var post = (Post)((StackPanel)sender).DataContext;
            ServiceLocator.Current.GetInstance<PostViewModel>().OpenBlogPostCommand.Execute(post);

            Debug.WriteLine("CategoryPage.xaml.cs | OnArticleTapped | PostId: {0}", post.Id);
        }

        private void AppBarIconButtonSearch_Click(object sender, EventArgs e)
        {
            ServiceLocator.Current.GetInstance<SearchViewModel>().Cleanup();
            _vm.NavigateCommand.Execute(string.Format("/View/SearchPage.xaml?category={0}", _vm.SelectedCategory.Title));
        }
    }
}